using System;
using System.Collections;
using PickGold.Charting;

namespace PickGold.Charting.ChartTypes
{
	internal class BarPointsDrawingOrderComparer : IComparer
	{
		private ChartArea _area;
		private Point3D _areaProjectionCenter = new Point3D(float.NaN, float.NaN, float.NaN);
		private bool _selection;

		public BarPointsDrawingOrderComparer(ChartArea area, bool selection, COPCoordinates coord)
		{
			this._area = area;
			this._selection = selection;
			if (area.DrawPointsToCenter(ref coord))
			{
				this._areaProjectionCenter = area.GetCenterOfProjection(coord);
				float x = this._areaProjectionCenter.X;
				this._areaProjectionCenter.X = this._areaProjectionCenter.Y;
				this._areaProjectionCenter.Y = x;
			}
		}

		public int Compare(object o1, object o2)
		{
			DataPoint3D pointd = (DataPoint3D)o1;
			DataPoint3D pointd2 = (DataPoint3D)o2;
			int num = 0;
			if (pointd.xPosition < pointd2.xPosition)
			{
				num = -1;
			}
			else if (pointd.xPosition > pointd2.xPosition)
			{
				num = 1;
			}
			else
			{
				if (pointd.yPosition < pointd2.yPosition)
				{
					num = 1;
				}
				else if (pointd.yPosition > pointd2.yPosition)
				{
					num = -1;
				}
				if (!float.IsNaN(this._areaProjectionCenter.Y))
				{
					double num2 = Math.Min(pointd.yPosition, pointd.height);
					double num3 = Math.Max(pointd.yPosition, pointd.height);
					double num4 = Math.Min(pointd2.yPosition, pointd2.height);
					double num5 = Math.Max(pointd2.yPosition, pointd2.height);
					if (this._area.IsBottomSceneWallVisible())
					{
						if ((num2 <= this._areaProjectionCenter.Y) && (num4 <= this._areaProjectionCenter.Y))
						{
							num *= -1;
						}
						else if (num2 <= this._areaProjectionCenter.Y)
						{
							num = 1;
						}
					}
					else if ((num3 >= this._areaProjectionCenter.Y) && (num5 >= this._areaProjectionCenter.Y))
					{
						num = num;
					}
					else if (num3 >= this._areaProjectionCenter.Y)
					{
						num = 1;
					}
					else
					{
						num *= -1;
					}
				}
				else if (!this._area.DrawPointsInReverseOrder())
				{
					num *= -1;
				}
			}
			if (pointd.xPosition != pointd2.xPosition)
			{
				if (!float.IsNaN(this._areaProjectionCenter.X))
				{
					if (((pointd.xPosition + (pointd.width / 2.0)) >= this._areaProjectionCenter.X) && ((pointd2.xPosition + (pointd2.width / 2.0)) >= this._areaProjectionCenter.X))
					{
						num *= -1;
					}
				}
				else if (this._area.IsBottomSceneWallVisible())
				{
					num *= -1;
				}
			}
			if (!this._selection)
			{
				return num;
			}
			return -num;
		}
	}
}

